package com.yili.oa.service.impl;

import com.yili.oa.dao.UserMapper;
import com.yili.oa.entity.UserSearch;
import com.yili.oa.models.User;
import com.yili.oa.models.UserExample;
import com.yili.oa.service.UserService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.vo.request.UserDataReq;
import com.yili.oa.vo.response.UserDataRes;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/3/21 0021
 * @email 200890949@qq.com
 */
@SuppressWarnings("ALL")
@Service
public class UserSvImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    @Override
    public List<User> getAllUser() {
        UserExample example = new UserExample();
        example.setOrderByClause("user_sort asc,user_update_time desc");
        return mapper.selectByExample(example);
    }

    @Override
    public Boolean add(UserDataReq userDataReq) {
        User createUser = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        User user = new User();
        BeanUtils.copyProperties(userDataReq,user);
        user.setUserId(UUID.randomUUID().toString());
        user.setUserCreateTime(new Date());
        user.setUserUpdateTime(new Date());
        user.setUserCreateUser(createUser.getUserId());
        user.setUserUpdateUser(createUser.getUserId());
        user.setUserSort("1");
        user.setUserStatus("0");
        try {
            mapper.insert(user);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public Boolean checkAccount(String account,String userId) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserAccountEqualTo(account);
        if (userId!=null) {
            criteria.andUserIdNotEqualTo(userId);
        }
        List<User> list = mapper.selectByExample(example);
        if(ListUtils.isNotNullOrEmpty(list)){
            return false;
        }
        return true;
    }

    @Override
    public Boolean update(UserDataReq userDataReq) {
        User createUser = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        User user = new User();
        BeanUtils.copyProperties(userDataReq,user);
        user.setUserUpdateTime(new Date());
        user.setUserUpdateUser(createUser.getUserId());
        try {
            mapper.updateByPrimaryKeySelective(user);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public UserDataRes getById(String userId) throws AjaxException{
        User user = mapper.selectByPrimaryKey(userId);
        if (user==null){
            throw new AjaxException("用户不存在");
        }
        UserDataRes udr = new UserDataRes();
        BeanUtils.copyProperties(user,udr);
        return udr;
    }

    @Override
    public Boolean del(List<String> userIds) throws AjaxException {
//        User user = mapper.selectByPrimaryKey(userId);
//        if (user==null){
//            throw new AjaxException("用户不存在");
//        }
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdIn(userIds);
        try {
            mapper.deleteByExample(example);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public List<User> searchUser(UserSearch search) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNoneBlank(search.getUserName())){
            criteria.andUserNameLike("%"+search.getUserName()+"%");
        }
        if (StringUtils.isNoneBlank(search.getUserMobilePhone())){
            criteria.andUserMobilePhoneLike("%"+search.getUserMobilePhone()+"%");
        }
        if (StringUtils.isNoneBlank(search.getUserApartment())){
            criteria.andUserApartmentLike("%"+search.getUserApartment()+"%");
        }
        if (StringUtils.isNotBlank(search.getUserStatus())){
            criteria.andUserStatusEqualTo(search.getUserStatus());
        }
        example.setOrderByClause("user_sort asc,user_update_time desc");
        return mapper.selectByExample(example);
    }

    @Override
    public Boolean updStatus(UserDataReq userDataReq) {
        User user = new User();
        BeanUtils.copyProperties(userDataReq,user);
        try {
            mapper.updateByPrimaryKeySelective(user);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public Boolean updRole(String id) {
        User user = mapper.selectByPrimaryKey(id);
        if (user==null){
            throw new AjaxException("用户不存在");
        }
        user.setUserRole(null);
        try {
            mapper.updateByPrimaryKey(user);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public User getByAccount(String account) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUserAccountEqualTo(account);
        List<User> users = mapper.selectByExample(example);
        User user = new User();
        if (ListUtils.isNotNullOrEmpty(users)){
            user=users.get(0);
        }
        return user;
    }
}
